Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\offsets.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::compiler::message::{Field, FieldType, Message, Referenced};
30
use crate::compiler::util::types::TypePathMap;
31
use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper};
32
use crate::gen::base::message::Templates;
33
use crate::gen::base::message_from_bytes::generate_from_bytes_impl;
34
use crate::gen::base::Error;
35
use crate::gen::codec::CodecMap;
36
use crate::gen::rust::util::{gen_where_clause, RustUtils};
37
use crate::gen::template::Template;
38
use itertools::Itertools;
39
40
const TEMPLATE: &[u8] = include_bytes!("offsets.template");
41
42
6
fn gen_message_offset_field(
43
6
    field: &Field,
44
6
    template: &Template,
45
6
    type_path_map: &TypePathMapper<DefaultTypeMapper>,
46
6
) -> String {
47
6
    let mut scope = template.scope();
48
6
    scope.var("name", &field.name);
49
1
    match &field.ty {
50
1
        FieldType::Ref(Referenced::Message(v)) => {
51
1
            scope.var("type_name", type_path_map.get(v));
52
1
            match field.optional {
53
1
                true => scope.render("decl", &["field", "msg_optional"]).unwrap(),
54
0
                false => scope.render("decl", &["field", "msg"]).unwrap(),
55
            }
56
        }
57
5
        _ => scope.render("decl", &["field"]).unwrap(),
58
    }
59
6
}
60
61
2
pub fn gen_message_offsets_decl(
62
2
    msg: &Message,
63
2
    codec_map: &CodecMap,
64
2
    type_path_map: &TypePathMap,
65
2
) -> Result<String, Error> {
66
2
    let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper);
67
2
    let mut templates = Templates {
68
2
        template: Template::compile(TEMPLATE).unwrap(),
69
2
        codec_map,
70
2
    };
71
2
    let where_clauses = msg
72
2
        .fields
73
2
        .iter()
74
6
        .map(|field| gen_where_clause(&templates.template, field, &type_path_map, "impl"))
75
2
        .join("");
76
2
    templates
77
2
        .template
78
2
        .var("msg_name", &msg.name)
79
2
        .var("generics", RustUtils::get_generics(msg, &type_path_map).to_string())
80
2
        .var("where_clauses", where_clauses);
81
2
    let fields = msg
82
2
        .fields
83
2
        .iter()
84
6
        .map(|field| gen_message_offset_field(field, &templates.template, &type_path_map))
85
2
        .join("");
86
2
    let mut code = templates.template.var("fields", fields).render("", &["decl"]).unwrap();
87
2
    code += "\n";
88
2
    code += &generate_from_bytes_impl::<RustUtils, _>(msg, &templates, &type_path_map, "impl")
?0
;
89
2
    Ok(code)
90
2
}